package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.CheckGroupAndCheckItemMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupAndCheckItem;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ceshi
 * @Title:检查项
 * @Package
 * @Description:
 * @date 2021/7/13/001319:02
 */
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService{

    @Autowired
    private CheckItemMapper mapper;
    @Autowired
    private CheckGroupAndCheckItemMapper checkGroupAndCheckItemMapper;

    @Override
    public void add(CheckItem checkItem) {
        mapper.insert(checkItem);
    }

    @Override
    public PageResult findByPage(Integer currentPage, Integer pageSize, String queryString) {

        Page<CheckItem> checkItems;

        if (queryString != "" && queryString != null) {
            //如果查询参数不为空
            Example example = new Example(CheckItem.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("code", "%" + queryString + "%").orLike("name", "%" + queryString + "%");
            PageHelper.startPage(currentPage, pageSize);
            checkItems = (Page<CheckItem>) mapper.selectByExample(example);
        } else {
            //如果参数为空
            PageHelper.startPage(currentPage, pageSize);
            checkItems = (Page<CheckItem>) mapper.selectAll();
        }
        return new PageResult(checkItems.getTotal(), checkItems.getResult());
    }

    @Override
    public void checkItemDelete(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return mapper.selectAll();
    }

    @Override
    public void edit(CheckItem checkItem) {
        mapper.updateByPrimaryKey(checkItem);
    }
}
